package com.hdax.dm.order.mappers;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hdax.dm.entity.order.Order;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
@Mapper
public interface OrderMapper extends BaseMapper<Order> {
    /**
     * 订单完成
     * @param orderNo 订单编号
     * @param tradeNo 支付宝交易号
     * @return
     */
    @Update("update dm_order set aliTradeNo=#{tradeNo},orderType=2 where orderNo=#{orderNo}")
    Boolean updateOrder(@Param("orderNo") String orderNo, @Param("tradeNo") String tradeNo);

    /**
     * 查询
     * @param orderNo
     * @return
     */
    @Select("select * from dm_order where orderNo=#{orderNo}")
    Order order(@Param("orderNo") String orderNo);

    /**
     * 超时操作
     * @param orderNo
     * @return
     */
    @Update("update dm_order set orderType=-1 where orderNo=#{orderNo}")
    Boolean updateOrderChao(@Param("orderNo") String orderNo);


    /**
     * 个人中心订单查询
     * @param orderType
     * @param orderTime
     * @param keyWord
     * @return
     */
    @Select({
            "<script>"+
                    "select * from dm_order " +
                    "<where>" +
                    "<if test=\" orderType!=null and  orderType!='' \" >"+
                    "orderType = #{orderType}"+
                    "</if>"+
                    "<if test='createTime!=0'>" +
                    "<if test='createTime==1'>" +
                    " and YEAR(createdTime) = YEAR(NOW())"+
                    "</if>"+
                    "<if test='createTime==2'>" +
                    " and createdTime between date_sub(now(),interval 6 month) and now()"+
                    "</if>"+
                    "</if>"+
                    "<if test=\" keyWord!=null and  keyWord!='' \" >"+
                    " and itemName like concat('%',#{keyWord},'%')"+
                    "</if>"+
                    "</where>"+
                    "</script>"
    })
    List<Order> likeOrder(@Param("orderType") String orderType, @Param("createTime") String orderTime, @Param("keyWord")String keyWord);

    /**
     * 取消订单的操作
     * @param orderNo
     * @return
     */
    @Update("update dm_order set orderType=-1 where orderNo=#{orderNo}")
    Boolean updateOrderOne(@Param("orderNo") String orderNo);
}
